﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Fodee.DataAccess
{
    public class DataAccessBase
    {
        private bool DoNonQuery(string procedureName, DataSet ds, DataBase.CommandOperation commandOperation, string tableName)
        {
            DataBase database = new DataBase();
            return database.ExcuteNonQuery(procedureName, ds, commandOperation, tableName);
        }

        public virtual bool Insert(DataSet ds, string tableName)
        {
            return DoNonQuery(GetStoreProcedureName(DataBase.CommandOperation.Insert,tableName), ds, DataBase.CommandOperation.Insert, tableName);
        }

        public virtual bool Update(DataSet ds, string tableName)
        {
            return DoNonQuery(GetStoreProcedureName(DataBase.CommandOperation.Update, tableName), ds, DataBase.CommandOperation.Update, tableName);
        }

        public virtual bool Delete(DataSet ds, string tableName)
        {
            //SqlParameter param = new SqlParameter("@ID", id);
            return DoNonQuery(GetStoreProcedureName(DataBase.CommandOperation.Delete, tableName), ds, DataBase.CommandOperation.Delete, tableName);
        }

        public virtual DataSet GetById(int id, string tableName)
        {
            DataBase database = new DataBase();
            SqlParameter param = new SqlParameter("@ID", id);
            return database.ExcuteQuery(GetStoreProcedureName(DataBase.CommandOperation.Query, tableName), new SqlParameter[] { param }, tableName);
        }

        public virtual DataSet GetAll(string tableName)
        {
            DataBase database = new DataBase();
            return database.ExcuteQuery(GetStoreProcedureName(DataBase.CommandOperation.None, tableName), tableName);
        }

        private string GetStoreProcedureName(DataBase.CommandOperation operation, string tableName)
        {
            string result = "P_" + tableName + "_";

            switch (operation)
            { 
                case DataBase.CommandOperation.Insert:
                    result +=  "I";
                    break;
                case DataBase.CommandOperation.Update:
                    result += "U";
                    break;
                case DataBase.CommandOperation.Delete:
                    result += "D";
                    break;
                case DataBase.CommandOperation.Query:
                    result += "S";
                    break;
                case DataBase.CommandOperation.None:
                    result += "All";
                    break;
            }

            return result;
        }
    }
}
